﻿using System;
using System.Collections.Generic;
using System.Linq;
using Model;
using DBUtility;
using MySql.Data.MySqlClient;

namespace DAL
{
    public class BatchRepository
    {
        // 添加批次
        public int AddBatch(Batch batch)
        {
            string sql = @"INSERT INTO batch 
                            (BatchId, ProductId, WorkOrderId, TotalProcessId, BatchType, Quantity, SubQuantity, 
                             GoodQuantity, NGQuantity, Unit, LockStatus, EventName, EventUser, EventTime, EventRemarks) 
                           VALUES 
                            (@BatchId, @ProductId, @WorkOrderId, @TotalProcessId, @BatchType, @Quantity, @SubQuantity, 
                             @GoodQuantity, @NGQuantity, @Unit, @LockStatus, @EventName, @EventUser, @EventTime, @EventRemarks)";

            MySqlParameter[] parameters = {
                new MySqlParameter("@BatchId", batch.BatchId),
                new MySqlParameter("@ProductId", batch.ProductId),
                new MySqlParameter("@WorkOrderId", batch.WorkOrderId),
                new MySqlParameter("@TotalProcessId", batch.TotalProcessId),
                new MySqlParameter("@BatchType", batch.BatchType),
                new MySqlParameter("@Quantity", batch.Quantity),
                new MySqlParameter("@SubQuantity", batch.SubQuantity),
                new MySqlParameter("@GoodQuantity", batch.GoodQuantity),
                new MySqlParameter("@NGQuantity", batch.NGQuantity),
                new MySqlParameter("@Unit", batch.Unit),
                new MySqlParameter("@LockStatus", batch.LockStatus),
                new MySqlParameter("@EventName", batch.EventName),
                new MySqlParameter("@EventUser", batch.EventUser),
                new MySqlParameter("@EventTime", batch.EventTime),
                new MySqlParameter("@EventRemarks", batch.EventRemarks)
            };

            return DBHelper.ExecuteNonQuery(sql, parameters);
        }

        // 删除批次
        public int DeleteBatch(int batchId)
        {
            string sql = "DELETE FROM batch WHERE BatchId = @BatchId";
            MySqlParameter[] parameters = { new MySqlParameter("@BatchId", batchId) };
            return DBHelper.ExecuteNonQuery(sql, parameters);
        }

        // 更新批次信息
        public int UpdateBatch(Batch batch)
        {
            string sql = @"UPDATE batch 
                            SET ProductId = @ProductId, 
                                WorkOrderId = @WorkOrderId, 
                                TotalProcessId = @TotalProcessId, 
                                BatchType = @BatchType, 
                                Quantity = @Quantity, 
                                SubQuantity = @SubQuantity, 
                                GoodQuantity = @GoodQuantity, 
                                NGQuantity = @NGQuantity, 
                                Unit = @Unit, 
                                LockStatus = @LockStatus, 
                                EventName = @EventName, 
                                EventUser = @EventUser, 
                                EventTime = @EventTime, 
                                EventRemarks = @EventRemarks 
                          WHERE BatchId = @BatchId";

            MySqlParameter[] parameters = {
                new MySqlParameter("@ProductId", batch.ProductId),
                new MySqlParameter("@WorkOrderId", batch.WorkOrderId),
                new MySqlParameter("@TotalProcessId", batch.TotalProcessId),
                new MySqlParameter("@BatchType", batch.BatchType),
                new MySqlParameter("@Quantity", batch.Quantity),
                new MySqlParameter("@SubQuantity", batch.SubQuantity),
                new MySqlParameter("@GoodQuantity", batch.GoodQuantity),
                new MySqlParameter("@NGQuantity", batch.NGQuantity),
                new MySqlParameter("@Unit", batch.Unit),
                new MySqlParameter("@LockStatus", batch.LockStatus),
                new MySqlParameter("@EventName", batch.EventName),
                new MySqlParameter("@EventUser", batch.EventUser),
                new MySqlParameter("@EventTime", batch.EventTime),
                new MySqlParameter("@EventRemarks", batch.EventRemarks),
                new MySqlParameter("@BatchId", batch.BatchId)
            };

            return DBHelper.ExecuteNonQuery(sql, parameters);
        }

        // 查询所有批次
        public List<Batch> GetAllBatches()
        {
            string sql = "SELECT * FROM batch";
            return DBHelper.ExecuteQuery(sql, null, MapBatch);
        }

        // 查询单个批次
        public Batch GetBatchById(int batchId)
        {
            string sql = "SELECT * FROM batch WHERE BatchId = @BatchId";
            MySqlParameter[] parameters = { new MySqlParameter("@BatchId", batchId) };
            var batches = DBHelper.ExecuteQuery(sql, parameters, MapBatch);
            return batches.FirstOrDefault();
        }

        // 映射数据库查询结果到 Batch 对象
        private Batch MapBatch(MySqlDataReader reader)
        {
            return new Batch
            {
                BatchId = (int)reader["BatchId"],
                ProductId = (int)reader["ProductId"],
                WorkOrderId = (int)reader["WorkOrderId"],
                TotalProcessId = (int)reader["TotalProcessId"],
                BatchType = reader["BatchType"].ToString(),
                Quantity = (int)reader["Quantity"],
                SubQuantity = (int)reader["SubQuantity"],
                GoodQuantity = (int)reader["GoodQuantity"],
                NGQuantity = (int)reader["NGQuantity"],
                Unit = reader["Unit"].ToString(),
                LockStatus = reader["LockStatus"].ToString(),
                EventName = reader["EventName"].ToString(),
                EventUser = reader["EventUser"].ToString(),
                EventTime = (DateTime)reader["EventTime"],
                EventRemarks = reader["EventRemarks"] is DBNull ? null : reader["EventRemarks"].ToString()
            };
        }
    }
}